package com.yx.frame.builder;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import com.yx.frame.builder.validate.ServiceBuilderConfigManager;
import com.yx.frame.builder.validate.Validator;

import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Combo;

public class MethodParmValidatorSelect extends Dialog {
	Shell window = null;
	protected Object result;
	protected Shell shell;
	List srcValidatorList;
	List validatorList;
	int selectIndex;
	Table parmTable;
	String[] validators;
	private Text methodName;
	private Label label;
	private Text parmName;
	private Label label_1;
	private Text parmCommet;
	private Label label_2;
	private Combo parmType;
	private Label lblSql;
	private Text parmSqlIdx;
	private Button parmIsArray;
	private Label label_3;
	private String methodType;
	private MethodParms methodParmsWin;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public MethodParmValidatorSelect(Shell parent, int style, Table parmTable,
			int selectIndex, Shell win,String methodType,MethodParms methodParmsWin) {
		super(parent, style);
		this.window = win;
		setText("参数设置:");
		this.parmTable = parmTable;
		this.selectIndex = selectIndex;
		this.methodType=methodType;
		this.methodParmsWin=methodParmsWin;
		if (this.selectIndex >= 0) {
			if (!"".equals(parmTable.getItem(selectIndex).getText(5))) {
				validators = parmTable.getItem(selectIndex).getText(5)
						.split(",");
				for (int i = 0; i < validators.length; i++) {
					Validator valt = ServiceBuilderConfigManager.getInstance()
							.getValidator(validators[i]);
					if (valt != null) {
						// throw new Exception("没有参数校验"+validators[i]);
						validators[i] = validators[i] + "-" + valt.getComment();
					}
				}
			}
		}
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
		shell.setSize(542, 469);
		shell.setText("参数设置:");

		srcValidatorList = new List(shell, SWT.BORDER | SWT.H_SCROLL
				| SWT.V_SCROLL);
		srcValidatorList.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				int selectIdx = srcValidatorList.getSelectionIndex();
				if (selectIdx < 0
						|| selectIdx >= srcValidatorList.getItemCount()) {
					return;
				}
				String validator = srcValidatorList.getItem(selectIdx);
				validator = validator.substring(0, validator.indexOf("-"));
				methodName.setText(ServiceBuilderConfigManager.getInstance()
						.getValidator(validator).getStaticMethodName());
			}
		});
		srcValidatorList.setBounds(10, 85, 237, 280);

		java.util.List<Validator> systemValidators = ServiceBuilderConfigManager
				.getInstance().getSystemValidators();
		java.util.List<Validator> userDefineValidators = ServiceBuilderConfigManager
				.getInstance().getUserDefineValidators();
		String[] items = new String[systemValidators.size()
				+ userDefineValidators.size()];
		for (int i = 0; i < systemValidators.size(); i++) {
			items[i] = systemValidators.get(i).getName() + "-"
					+ systemValidators.get(i).getComment();
		}
		for (int i = 0; i < userDefineValidators.size(); i++) {
			items[i + systemValidators.size()] = userDefineValidators.get(i)
					.getName() + "-" + userDefineValidators.get(i).getComment();
		}
		srcValidatorList.setItems(items);

		validatorList = new List(shell, SWT.BORDER | SWT.H_SCROLL
				| SWT.V_SCROLL);
		validatorList.setBounds(288, 85, 236, 280);
		

		Button button = new Button(shell, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String name = parmName.getText().trim();
				if ("".equals(name)) {
					return;
				}
				String type = parmType.getText().trim();
				if ("".equals(type)) {
					return;
				}
				int sqlIdx = 0;
				if (!"java".equals(methodType)) {
					try {
						sqlIdx = Integer.parseInt(parmSqlIdx.getText().trim());
					} catch (Exception ex) {
						return;
					}
				}
				String isMutiList = "false";
				if (parmIsArray.getSelection()) {
					isMutiList = "true";
				}
				for (int i = 0; i < parmTable.getItemCount(); i++) {
					if(i==selectIndex)continue;
					if (name.equals(parmTable.getItem(i).getText(1))) {
						parmName.setText("");
						return;
					}
				}
				String comment = parmCommet.getText().trim();
				
				String validatorStr = "";
				String[] items = validatorList.getItems();
				for (int i = 0; i < items.length; i++) {
					validatorStr = validatorStr
							+ items[i].substring(0, items[i].indexOf("-"))
							+ ",";
				}
				if (validatorStr.endsWith(",")) {
					validatorStr = validatorStr.substring(0,
							validatorStr.length() - 1);
				}
				
				if (selectIndex >= 0) {
					methodParmsWin.editParm(selectIndex, name, type, isMutiList, ""
							+ sqlIdx, validatorStr, comment);
				}else{
					methodParmsWin.addParm(parmTable.getItemCount(), name, type, isMutiList, ""+sqlIdx,validatorStr, comment);
				}				
				shell.close();
				shell.dispose();

			}
		});
		button.setBounds(360, 395, 80, 27);
		button.setText("确定");

		Button button_1 = new Button(shell, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.close();
				shell.dispose();
			}
		});
		button_1.setBounds(444, 395, 80, 27);
		button_1.setText("取消");

		Button button_2 = new Button(shell, SWT.NONE);
		button_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int selectIdx = srcValidatorList.getSelectionIndex();
				if (selectIdx < 0
						|| selectIdx >= srcValidatorList.getItemCount()) {
					return;
				}
				String validator = srcValidatorList.getItem(selectIdx);
				String[] items = validatorList.getItems();
				boolean isExist = false;
				for (int i = 0; i < items.length; i++) {
					if (items[i].equals(validator)) {
						isExist = true;
						break;
					}
				}
				if (!isExist) {
					validatorList.add(validator);
				}
			}
		});
		button_2.setBounds(253, 149, 29, 27);
		button_2.setText(">");

		Button button_3 = new Button(shell, SWT.NONE);
		button_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int selectIdx = validatorList.getSelectionIndex();
				if (selectIdx < 0 || selectIdx >= validatorList.getItemCount()) {
					return;
				}
				if (selectIdx >= 0) {
					validatorList.remove(selectIdx);
				}
			}
		});
		button_3.setText("<");
		button_3.setBounds(253, 203, 29, 27);

		methodName = new Text(shell, SWT.BORDER);
		methodName.setEditable(false);
		methodName.setBounds(10, 370, 514, 23);

		label = new Label(shell, SWT.NONE);
		label.setText("参数名称：");
		label.setBounds(10, 8, 61, 17);

		parmName = new Text(shell, SWT.BORDER);
		parmName.setBounds(81, 5, 166, 23);

		label_1 = new Label(shell, SWT.NONE);
		label_1.setText("参数描述：");
		label_1.setBounds(284, 8, 61, 17);

		parmCommet = new Text(shell, SWT.BORDER);
		parmCommet.setBounds(355, 5, 169, 23);

		label_2 = new Label(shell, SWT.NONE);
		label_2.setText("参数类型：");
		label_2.setBounds(10, 36, 61, 17);

		parmType = new Combo(shell, SWT.NONE);
		parmType.setItems(new String[] { "String", "Int", "Double", "Long",
				"Date", "Clob", "ZipClob" });
		parmType.setBounds(81, 32, 89, 25);
		parmType.select(0);

		lblSql = new Label(shell, SWT.NONE);
		lblSql.setText("SQL序号：");
		lblSql.setBounds(284, 36, 61, 17);

		parmSqlIdx = new Text(shell, SWT.BORDER);
		parmSqlIdx.setBounds(355, 33, 34, 23);

		parmIsArray = new Button(shell, SWT.CHECK);
		parmIsArray.setText("数组格式");
		parmIsArray.setBounds(457, 36, 69, 17);

		label_3 = new Label(shell, SWT.NONE);
		label_3.setText("参数校验：");
		label_3.setBounds(10, 62, 61, 17);
		if ("query".equals(methodType)) {
			parmIsArray.setSelection(false);
			parmIsArray.setVisible(false);
			parmSqlIdx.setEditable(true);

		} else if ("java".equals(methodType)) {
			parmIsArray.setSelection(false);
			parmIsArray.setEnabled(true);
			parmSqlIdx.setVisible(false);
			lblSql.setVisible(false);

		} else {
			parmIsArray.setSelection(false);
			parmIsArray.setEnabled(true);
			parmSqlIdx.setEditable(true);
		}
		if (this.selectIndex >= 0) {
			TableItem item=parmTable.getItem(selectIndex);
			parmName.setText(item.getText(1));
			parmType.setText(item.getText(2));
			if("true".equals(item.getText(3))){
				parmIsArray.setSelection(true);
			}else{
				parmIsArray.setSelection(false);
			}
			parmSqlIdx.setText(item.getText(4));
			if (validators != null) {
				validatorList.setItems(validators);
			}
			parmCommet.setText(item.getText(6));
		}		
	}
}
